package com.yfproject.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yfproject.models.Labels;
import com.yfproject.models.custommodels.LevelCount2;
import com.yfproject.service.impl.LabelServiseImpl;



@RestController
@RequestMapping("/lable")
public class LableController {
	@Autowired
	LabelServiseImpl labelServiseImpl;
		
	/**
	 * 根据父标签id查询、分页所有子标签信息
	 * @param parentId
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/queryAndPageLabelsByParentId",method=RequestMethod.POST)
	public Page<Labels> queryAndPageLabelsByParentId(
			@RequestParam(value = "parentId") Long parentId, 
			@RequestParam(value = "page", defaultValue = "0") Integer page, 
			@RequestParam(value = "size", defaultValue = "15") Integer size) {
		Sort sort = new Sort(Sort.Direction.ASC, "id");
		Pageable pageable = new PageRequest(page, size, sort);
		Page<Labels> pageLabels = labelServiseImpl.qryParentLabel(parentId, pageable);
		return pageLabels;
	}
	
	/**
	 * 根据父标签id查询、不分页所有子标签信息
	 * @param parentId
	 * @return
	 */
	@RequestMapping(value="/queryAndNotPageLabelsByParentId",method=RequestMethod.POST)
	public Page<Labels> queryAndNotPageLabelsByParentId(
			@RequestParam(value = "parentId") Long parentId) {
		Sort sort = new Sort(Sort.Direction.ASC, "id");
		int size = labelServiseImpl.qryParentLabel(parentId).size();
		Pageable pageable = new PageRequest(0, size, sort);
		Page<Labels> pageLabels = labelServiseImpl.qryParentLabel(parentId, pageable);
		return pageLabels;		
	}
	
	/**
	 * 根据父标签id查询、分页可用标签信息
	 * @param parentId
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/queryAndPageAvailableByParentId",method=RequestMethod.POST)
	public Page<Labels> queryAndPageAvailableByParentId(
			@RequestParam(value = "parentId") Long parentId, 
			@RequestParam(value = "page", defaultValue = "0") Integer page, 
			@RequestParam(value = "size", defaultValue = "15") Integer size) {
		Sort sort = new Sort(Sort.Direction.ASC, "id");
		Pageable pageable = new PageRequest(page, size, sort);
		Page<Labels> pageLabels = labelServiseImpl.queryByParentIdAndStatus(parentId,2,pageable);
		return pageLabels;		
	}
	
	/**
	 * 根据父标签id查询、不分页可用标签信息
	 * @param ParentId
	 * @return
	 */
	@RequestMapping(value="/queryAndNotPageAvailableByParentId",method=RequestMethod.POST)
	public Page<Labels> queryAndNotPageAvailableByParentId(
			@RequestParam(value = "parentId") Long parentId) {
		Sort sort = new Sort(Sort.Direction.ASC, "id");
		int size = labelServiseImpl.queryByParentIdAndStatus(parentId,2).size();
		Pageable pageable = new PageRequest(0, size, sort);
		Page<Labels> pageLabels = labelServiseImpl.queryByParentIdAndStatus(parentId,2,pageable);
		return pageLabels;		
	}
	
	/**
	 * 根据标签名模糊查询、分页所有标签信息
	 * @param name
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/findAndPageByName",method=RequestMethod.POST)
	public Page<Labels> findAndPageByName(
			@RequestParam(value = "name") String name, 
			@RequestParam(value = "page", defaultValue = "0") Integer page, 
			@RequestParam(value = "size", defaultValue = "15") Integer size) {
		Sort sort = new Sort(Sort.Direction.ASC, "id");
		Pageable pageable = new PageRequest(page, size, sort);
		Page<Labels> pageLabels = labelServiseImpl.findByName(name, pageable);
		return pageLabels;	
	}
	
	/**
	 * 根据标签名模糊查询、不分页所有标签信息
	 * @param name
	 * @return
	 */
	@RequestMapping(value="/findAndNotPageByName",method=RequestMethod.POST)
		public Page<Labels> findAndNotPageByName(
			@RequestParam(value = "name") String name) {
		Sort sort = new Sort(Sort.Direction.ASC, "id");
		int size = labelServiseImpl.findByName(name).size();
		Pageable pageable = new PageRequest(0, size, sort);
		Page<Labels> pageLabels = labelServiseImpl.findByName(name, pageable);
		return pageLabels;	
	}
	
	/**
	 * 根据标签名模糊查询、分页可用标签信息
	 * @param name
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/findAndPageAvailableByName",method=RequestMethod.POST)
	public Page<Labels> findAndPageAvailableByName(
			@RequestParam(value = "name") String name, 
			@RequestParam(value = "page", defaultValue = "0") Integer page, 
			@RequestParam(value = "size", defaultValue = "15") Integer size) {
		Sort sort = new Sort(Sort.Direction.ASC, "id");
		Pageable pageable = new PageRequest(page, size, sort);
		Page<Labels> pageLabels = labelServiseImpl.findByNameAndStatus(name, 2, pageable);
		return pageLabels;	
	}

	/**
	 * 根据标签名模糊查询、不分页可用标签信息
	 * @param name
	 * @return
	 */
	@RequestMapping(value="/findAndNotPageAvailableByName",method=RequestMethod.POST)
		public Page<Labels> findAndNotPageAvailableByName(
			@RequestParam(value = "name") String name) {
		Sort sort = new Sort(Sort.Direction.ASC, "id");
		int size = labelServiseImpl.findByNameAndStatus(name, 2).size();
		Pageable pageable = new PageRequest(0, size, sort);
		Page<Labels> pageLabels = labelServiseImpl.findByNameAndStatus(name,2,pageable);
		return pageLabels;	
	}
	
	@RequestMapping("/query/{id}")
	/**
	 * 根据标签id查询标签信息
	 * @param id
	 * @return
	 */
	public List<Labels> getLabelInfoById(@PathVariable Long id) {
		
		return labelServiseImpl.qryLabel(id);
		
	}
	
	/**
	 * 根据父标签id查询所有子标签信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/queryByParentId/{id}")
	public List<Labels> queryByParentId(@PathVariable Long id) {
		
		return labelServiseImpl.qryParentLabel(id);
		
	}
	
	/**
	 * 根据父标签id查询所有可用标签信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/queryAvailableByParentId/{id}")
	public List<Labels> queryAvailableByParentId(@PathVariable Long id) {
		
		return labelServiseImpl.queryByParentIdAndStatus(id,2);
		
	}
	
	/**
	 * 根据标签等级获取所有标签信息
	 * @param level
	 * @return
	 */
	@RequestMapping("/queryByLevel/{level}")
	public List<LevelCount2> queryByLevel(@PathVariable int level) {
		return labelServiseImpl.qryAndCountByLevel(level);
		
	}
	
	/**
	 * 修改name，description	 
	 * @param id
	 * @param name
	 * @param description
	 */
	@RequestMapping(value="/modify",method=RequestMethod.POST)
	public void modifyLabels(@RequestParam(value="id",required=true) Long id,
		@RequestParam(value="name",required=true) String name,
		@RequestParam(value="description",required=false) String description) {
		if(description!=null) {
			labelServiseImpl.modifyLabel(id, name, description);
		}else {
			labelServiseImpl.modifyLabel( id, name);
		}	
	}
	
	/**
	 * 根据标签名模糊查询所有标签信息
	 * @param name
	 * @return
	 */
	@RequestMapping("/findByName/{name}")
	public List<Labels> findByName(@PathVariable String name) {
		return labelServiseImpl.findByName(name);	
	}
}
